package com.wink_172.library.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.view.View.OnFocusChangeListener
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import com.wink_172.library.R
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.DisplayUtil

/**
 * 搜索栏标题控件
 * 支持多种样式
 * 对外提供事件回调  其余逻辑自行封装处理即可
 */
open class SearchBarView : RelativeLayout, View.OnClickListener {
    //    public SearchBarViewBinding binding;
    open lateinit var left_icon: ImageView
    open  lateinit var btn_clean: ImageView
    open lateinit var input_view: CustomEditText
    open lateinit var btn_right: TextView
    fun setCallback(callback: ICallback?) {
        mCallback = callback
    }

    var mCallback: ICallback? = null

    interface ICallback {
        fun onSendEvent(event: Int, content: String?)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        initView()
    }

    override fun onClick(v: View) {
        if (v.id == R.id.btn_clean) {
            input_view!!.setText("")
            mCallback!!.onSendEvent(Constants.EventEnum.清空.ordinal, null)
        } else if (v.id == R.id.left_icon) {
            mCallback!!.onSendEvent(Constants.EventEnum.返回.ordinal, null)
        }else if(v.id==R.id.btn_right){
            val input = input_view?.getText().toString()
            mCallback!!.onSendEvent(Constants.EventEnum.搜索.ordinal, input)
        }
    }

     fun initView() {
        left_icon = findViewById(R.id.left_icon)
        input_view = findViewById(R.id.input_view)
        btn_clean = findViewById(R.id.btn_clean)
        btn_right = findViewById(R.id.btn_right)
        btn_clean?.setVisibility(GONE)
        btn_clean?.setOnClickListener(this)
        left_icon?.setOnClickListener(this)
         btn_right?.setOnClickListener(this)
        input_view?.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            val input = input_view?.getText().toString()
            mCallback!!.onSendEvent(Constants.EventEnum.搜索.ordinal, input)
            true
        })
        input_view?.setOnFocusChangeListener(OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                // 此处为得到焦点时的处理内容
                input_view?.setActivated(true)
            } else {
                // 此处为失去焦点时的处理内容
                input_view?.setActivated(false)
            }
        })
        input_view?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                val ss = s.toString()
                if (TextUtils.isEmpty(ss)) {
                    btn_clean?.setVisibility(GONE)
                    //                    initHistoryList();
                } else {
                    btn_clean?.setVisibility(VISIBLE)
                }
                mCallback!!.onSendEvent(Constants.EventEnum.输入框文字变动.ordinal, ss)
            }
        })
    }

    /**
     * 更新输入框左边图标
     * R.mipmap.ic_search
     * */
    fun updateDrawableLeft(mipmapId:Int){
        input_view.setCompoundDrawables(null, null, null, null)

        val drawable1: Drawable = org.xutils.x.app().getResources().getDrawable(mipmapId)
        drawable1.setBounds(0, 0, DisplayUtil.dp2px(context,18f), DisplayUtil.dp2px(context,18f))
        input_view.setCompoundDrawables(drawable1, null, null, null)
    }

    /**
     * 更新输入框的背景
     * R.drawable.shap17
     * */
    fun updateInputBg(resId:Int){
        input_view.setBackgroundResource(resId)
    }

    /**
     * 不要左按钮 右按钮
     * **/
    fun hideLeftRightBtn(){
        left_icon.visibility=View.GONE
        btn_right.visibility=View.GONE
    }

    /**
     * 右按钮
     * **/
    fun hideRightBtn(){
        btn_right.visibility=View.GONE
    }

    /**
     * */
    fun getViewArray(){

    }
}